package com.ruoyi.contract.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ice.common.dto.IceBaseDto;
import com.ice.common.model.IceShowNode;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.contract.domain.*;
import com.ruoyi.contract.service.ConRuleService;
import com.ruoyi.contract.service.IceBaseService;
import com.ruoyi.contract.service.IceConfService;
import com.ruoyi.contract.service.IceFlowService;
import com.ruoyi.ice.domain.StringFlow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/contract/ice-conf")
public class IceConfController extends BaseController {

    @Autowired
    IceConfService iceConfService;

    @Autowired
    IceFlowService iceFlowService;

    @Autowired
    ConRuleService conRuleService;

    @Autowired
    IceBaseService iceBaseService;

    @PostMapping("/save")
    @Log(title = "新增节点", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('contract:rules:edit')")
    public AjaxResult save(@RequestBody IceConf conf) throws Exception {

        if(StringUtils.isNull(conf.getSelectId())){
            return AjaxResult.error("请选择父节点");
        }

        if(StringUtils.isNull(conf.getRuleId())){
            return AjaxResult.error("请选择规则");
        }

        ConRule rule = conRuleService.getById(conf.getRuleId());

        if(conf.getNodeType() == 4){
            //关系判断
            try {
                if(StringUtils.isNull(conf.getType())){
                    return AjaxResult.error("请选择关系类型");
                }

                if(!iceConfService.insertRelation(conf,rule)){
                    throw new Exception("新增失败");
                }
            }catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }

        }else if(conf.getNodeType()==5){
            //新增判断类型
            try {
                if(StringUtils.isNull(conf.getFlowId())){
                    return AjaxResult.error("请选择判断节点");
                }

                IceFlow flow = iceFlowService.getById(conf.getFlowId());


                if(!iceConfService.insertFlow(conf,flow,rule)){
                    throw new Exception("新增失败");
                }
            }catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
        } else if (conf.getNodeType()==6){
            //新增结果判断
            try {

                if(!iceConfService.saveResult(conf,rule)){
                    throw new Exception("新增失败");
                }

            }catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }


        } else {
            return AjaxResult.error("节点类型错误");
        }

        return AjaxResult.success();

    }

    @PutMapping("/move")
    @Log(title = "移动节点", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('contract:rules:edit')")
    public AjaxResult move(@RequestBody IceEditNode node){

        try {
            return iceConfService.move(node);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @DeleteMapping("/remove")
    @Log(title = "删除节点", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('contract:rules:edit')")
    public AjaxResult remove(@RequestBody IceEditNode node){

        //System.out.println(JSON.toJSONString(node));

        try {
            return iceConfService.remove(node);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }

    }

    @PutMapping("/exchange")
    @PreAuthorize("@ss.hasPermi('contract:rules:edit')")
    @Log(title = "转换节点", businessType = BusinessType.UPDATE)
    public AjaxResult exchange(@RequestBody IceConf conf){

        if(StringUtils.isNull(conf.getRuleId())){
            return AjaxResult.error("请选择规则");
        }

        if(StringUtils.isNull(conf.getSelectId())){
            return AjaxResult.error("请选择节点");
        }

        ConRule rule = conRuleService.getById(conf.getRuleId());

        IceBase iceBase = iceBaseService.getById(rule.getIceId());

        //如果是根节点的转换,不判断是否选择父节点和节点序号为空的问题

        if(!iceBase.getConfId().equals(conf.getSelectId())){

            if(StringUtils.isNull(conf.getParentId())){
                return AjaxResult.error("请选择父节点");
            }

            if(StringUtils.isNull(conf.getIndex())){
                return AjaxResult.error("节点序号为空");
            }
        }


        if(conf.getNodeType() == 4){
            //关系判断
            try {
                if(StringUtils.isNull(conf.getType())){
                    return AjaxResult.error("请选择关系类型");
                }

                if(!iceConfService.exchangeRelation(conf,rule)){
                    throw new Exception("转换为关系失败");
                }

            }catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }

        }else if(conf.getNodeType()==5){
            //新增判断类型
            try {
                if(StringUtils.isNull(conf.getFlowId())){
                    return AjaxResult.error("请选择判断节点");
                }

                IceFlow flow = iceFlowService.getById(conf.getFlowId());

                if(!iceConfService.exchangeFlow(conf,flow,rule)){
                    throw new Exception("转换为判断失败");
                }

            }catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
        } else if (conf.getNodeType()==6){
            //新增结果判断
            try {

                //if(!iceConfService.saveResult(conf,rule)){
                //    throw new Exception("新增失败");
                //}

                if(!iceConfService.exchangeResult(conf,rule)){
                    throw new Exception("转换为结果失败");
                }

            }catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }


        } else {
            return AjaxResult.error("节点类型错误");
        }

        return AjaxResult.success();

    }

    @PutMapping("/edit")
    @PreAuthorize("@ss.hasPermi('contract:rules:edit')")
    @Log(title = "修改节点", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody IceShowNode node,Long iceId){
        try {
            return iceConfService.edit(node,iceId);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PutMapping("/release")
    @PreAuthorize("@ss.hasPermi('contract:rules:edit')")
    @Log(title = "发布规则", businessType = BusinessType.UPDATE)
    public AjaxResult release(Long iceId){
        try {
            return iceConfService.release(iceId);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PutMapping("/clean")
    @PreAuthorize("@ss.hasPermi('contract:rules:edit')")
    @Log(title = "清空规则", businessType = BusinessType.UPDATE)
    public AjaxResult clean(Long iceId){
        try {
            return iceConfService.clean(iceId);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
